<?php

/**
 * This is for searching...
 */
class Validations_Search {

    public function doSearch($searchCriteria) {
        $db = new Ads_Model_Rentdetails();
        $dbData = $db->fetchAll();
        $searchArray = array();
        if (sizeof($dbData) == 0) {
            return $searchArray;
        }
        foreach ($dbData as $property) {
            $decodeJson = json_decode($property['details']);
//            $cityMatched = false;
            $stateMatched = false;
            $zipcodeMatched = false;
            $propertyTypeMatched = false;
            $rentalPeiodMatched = false;
            $finalMatchDec = false;
            $furnishTypeMatched = false;
            $roomMatched = false;
            $areaMatched = false;
            $rentMatched = false;
            $fncount = 0;

            #1 city compare
//            if ($searchCriteria['city'] == -1) {
//                $cityMatched = true;
//                $fncount++;
//            } else if ($searchCriteria['city'] == $db->getCityIdOfPropertyJson($decodeJson)) {
//                $cityMatched = true;
//            }
            #2 state compare
            if (is_null($searchCriteria['state'])) {
                $stateMatched = true;
                $fncount++;
            } else if (in_array($db->getStateIdofPropertyJson($decodeJson), $searchCriteria['state'])) {
                $stateMatched = true;
            } else {
                $stateMatched = false;
                $fncount++;
            }

            #3 zipcode compare
            if (is_null($searchCriteria['zipcode'])) {
                $zipcodeMatched = true;
                $fncount++;
            } else if (in_array($db->getZipIdOfPropertyJson($decodeJson), $searchCriteria['zipcode'])) {
                $zipcodeMatched = true;
            } else {
                $zipcodeMatched = false;
                $fncount++;
            }

            #4 property type compare            
            if ($searchCriteria['propertyType'] == null) {
                $propertyTypeMatched = true;
                $fncount++;
            } else if (in_array($db->getProprtyTypeIdofPropertyJson($decodeJson), $searchCriteria['propertyType'])) {
                $propertyTypeMatched = true;
            } else {
                $propertyTypeMatched = false;
                $fncount++;
            }

            #5 rental period compare
            if ($searchCriteria['rentalPeriod'] == null) {
                $rentalPeiodMatched = true;
                $fncount++;
            } else if (in_array($db->getRentalPeriodIdOfPropertyJson($decodeJson), $searchCriteria['rentalPeriod'])) {
                $rentalPeiodMatched = true;
            } else {
                $rentalPeiodMatched = false;
                $fncount++;
            }

            #6 furnish type compare
            if ($searchCriteria['furnishType'] == null) {
                $furnishTypeMatched = true;
                $fncount++;
            } else if (in_array($db->getFurnishTypeIdOfPropertyJson($decodeJson), $searchCriteria['furnishType'])) {
                $furnishTypeMatched = true;
            } else {
                $furnishTypeMatched = false;
                $fncount++;
            }

            #7 room compare            
            $roomsInData = $db->getRoomOfPropertyJson($decodeJson);
            $searchingRooms = $searchCriteria['rooms'];
            if ($searchingRooms == null) {
                $roomMatched = true;
                $fncount++;
            } else if (in_array($roomsInData, $searchingRooms)) {
                $roomMatched = true;
            } else {
                $roomMatched = false;
                $fncount++;
            }

//            if ($searchingRooms == 0) {
//                $roomMatched = true;
//                $fncount++;
//            } else if ($searchingRooms == $roomsInData) {
//                $roomMatched = true;
//            } else if ($searchingRooms == 10 && $searchingRooms <= $roomsInData) {
//                $roomMatched = true;
//            } else {
//                $roomMatched = false;
//            }
            #8 area compare 1=>1-50, 2=>51-100, 3=>101-150, 4=>151-200, 5=>201-250,6=>251+
//            $searchingArea = $searchCriteria['area'];
            $size1 = $searchCriteria['size1'];
            $size2 = $searchCriteria['size2'];
            if ($size1 == 0 && $size2 == 0) {
                $areaMatched = true;
                $fncount++;
            } else {
                $areaInData = $db->getAreaOfPropertyJson($decodeJson);
                $areaMatched = $this->isBetween($areaInData, $size1, $size2);
            }
//            switch ($searchingArea) {
//                case 0:
//                    $areaMatched = true;
//                    $fncount++;
//                    break;
//                case 1:
//                    $areaMatched = $this->isBetween($areaInData, 1, 50);
//                    break;
//                case 2:
//                    $areaMatched = $this->isBetween($areaInData, 51, 100);
//                    break;
//                case 3:
//                    $areaMatched = $this->isBetween($areaInData, 101, 150);
//                    break;
//                case 4:
//                    $areaMatched = $this->isBetween($areaInData, 151, 200);
//                    break;
//                case 5:
//                    $areaMatched = $this->isBetween($areaInData, 201, 250);
//                    break;
//                case 6:
//                    $areaMatched = ($areaInData >= 251) ? true : false;
//                default:
//                    break;
//            }
            #9 rent compare 1=>1-500, 2=>501-1000, 3=>1001-1500, 4=>1501-2000, 5=>2001-2500,6=>2501+
//            $searchingRent = $searchCriteria['rent'];            
            $rent1 = $searchCriteria['rent1'];
            $rent2 = $searchCriteria['rent2'];
            if ($rent1 == 0 && $rent2 == 0) {
                $rentMatched = true;
                $fncount++;
            } else {
                $rentInData = $db->getRentOfPropertyJson($decodeJson);
                $rentMatched = $this->isBetween($rentInData, $rent1, $rent2);
            }
//            switch ($searchingRent) {
//                case 0:
//                    $rentMatched = true;
//                    $fncount++;
//                    break;
//                case 1:
//                    $rentMatched = $this->isBetween($rentInData, 1, 500);
//                    break;
//                case 2:
//                    $rentMatched = $this->isBetween($rentInData, 501, 1000);
//                    break;
//                case 3:
//                    $rentMatched = $this->isBetween($rentInData, 1001, 1500);
//                    break;
//                case 4:
//                    $rentMatched = $this->isBetween($rentInData, 1501, 2000);
//                    break;
//                case 5:
//                    $rentMatched = $this->isBetween($rentInData, 2001, 2500);
//                    break;
//                case 6:
//                    $rentMatched = ($rentInData >= 2501) ? true : false;
//                default:
//                    break;
//            }
            #final dec                        
            if ($fncount == 8) {
                $finalMatchDec = false;
            } else {
                $finalMatchDec = true;
            }

            if ($finalMatchDec && $stateMatched && $zipcodeMatched && $propertyTypeMatched && $rentalPeiodMatched && $furnishTypeMatched && $roomMatched && $areaMatched && $rentMatched) {
                array_push($searchArray, $property);
            }
        }
        return $searchArray;
    }

    /**
     * 
     * @param type $compareValue
     * @param type $value1
     * @param type $value2
     */
    public function isBetween($compareValue, $value1, $value2) {
        $isBetween = false;
        if ($compareValue >= $value1 && $compareValue <= $value2) {
            $isBetween = true;
        }
        return $isBetween;
    }

    public function mapSearch($id) {
        $rentDetailsDb = new Ads_Model_Rentdetails();
        $searchArray = array();
        $details = $rentDetailsDb->fetchAll();
        foreach ($details as $property) {
            $djson = json_decode($property['details']);
            $proDetails = $djson->details;
            if ($proDetails->state == $id) {
                array_push($searchArray, $property);
            }
        }
        return $searchArray;
    }

}